<?php

namespace app\admin\controller\dts;

use app\common\controller\Backend;
use DateTime;

/**
 * 设备管理
 *
 * @icon fa fa-circle-o
 */
class Equipment extends Backend
{
    protected $noNeedRight = ['selectpage'];

    public function index() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $name = ''; $deviceNo = '';
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $filter = $this->request->get("filter", '');
            if($filter) {
                $filter = json_decode($filter, true);
                $name = isset($filter['name']) ? $filter['name'] : '';
                $deviceNo = isset($filter['deviceNo']) ? $filter['deviceNo'] : '';
            }
            $woapi = new \fast\Woapi;
            $list = $woapi->devicePage($limit / 10, $limit, $name, $deviceNo);
            foreach($list['content'] as $key => $items) {
                $list['content'][$key]['id'] = $items['deviceNo'];
                $dateTime1 = new DateTime($items['lastActiveTime']);
                $list['content'][$key]['lastActiveTime'] = $dateTime1->format('Y-m-d H:i:s');
                $dateTime2 = new DateTime($items['createTime']);
                $list['content'][$key]['createTime'] = $dateTime2->format('Y-m-d H:i:s');
            }

            $result = array("total" => $list['total'], "rows" => $list['content']);

            return json($result);
        }

        return $this->view->fetch();
    }

    public function selectpage()
    {
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");
        $deviceNo = $this->request->request("deviceNo");

        $woapi = new \fast\Woapi;
        $list = $woapi->devicePage($page, $pagesize, '', $deviceNo);
        return ['total' => $list['total'], 'list' => $list['content']];
    }

    public function add() {
        
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $woapi = new \fast\Woapi;
                $result = $woapi->deviceCreate($params['name'], $params['tag'], $params['deviceNo'], $params['forceEmptyDevice'], $params['password']);

                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        return $this->view->fetch();
    }

    public function edit($ids = null) {

        $woapi = new \fast\Woapi;
        $row = $woapi->deviceDetail($ids);

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $woapi = new \fast\Woapi;
                $result = $woapi->deviceUpdate($params['name'], $params['tag'], $params['deviceNo']);

                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $woapi = new \fast\Woapi;
            $result = $woapi->deviceDelete($ids);

            $this->success();
        }
        $this->error(__('You have no permission'));
    }
}